package javamobile.billsplit;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.ImageView;

import android.view.View;
import android.widget.Button;


public class AddPhotoActivity extends Activity {
	
	public static final String PREFS_NAME = "MyPrefsFile";
	private static int RESULT_LOAD_IMAGE = 1;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_photo);
    
        Button load_button = (Button) findViewById(R.id.load_btn_ADDPHOTO);
        load_button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				Intent i = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				
				startActivityForResult(i, RESULT_LOAD_IMAGE);
			}
		});
    
    }

    
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	
		if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };

			Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			cursor.moveToFirst();

			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			String picturePath = cursor.getString(columnIndex);
			cursor.close();
			
			ImageView imageView = (ImageView) findViewById(R.id.imgView_ADDPHOTO);
			imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
			
	    	SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
	    	SharedPreferences.Editor editor = settings.edit();
	    	editor.putString("galary", picturePath);
	    	editor.commit();
	    	
	    	finish();
		}
    
    
    }

    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.take_photo, menu);
        return true; 
    }
}
